using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace FrameWork.Kit
{
    public class PlayerToolkitView : MonoBehaviour
    {
        [SerializeField] private Button[] actionBTNS; // 工具栏按钮数组

        //
        private void Awake()
        {
            // 为每个按钮添加点击事件
            foreach (Button actionBTN in actionBTNS)
            {
                // 使用匿名方法 委托，确保每次点击到正确按钮
                actionBTN.onClick.AddListener(() => IconClick(actionBTN));
            }
        }

        private void OnEnable()
        {
            IconClick(actionBTNS[0]);
        }

        // 
        private void Update()
        {
            KeyboardSelectIcon();
        }

        /// <summary>
        /// 按键选择工具
        /// </summary>
        private void KeyboardSelectIcon()
        {
            if (PlayerInput.Instance.GetIsToolkit1)
            {
                IconClick(actionBTNS[0]);
            }
            else if (PlayerInput.Instance.GetIsToolkit2)
            {
                IconClick(actionBTNS[1]);
            }
            else if (PlayerInput.Instance.GetIsToolkit3)
            {
                IconClick(actionBTNS[2]);
            }
            else if (PlayerInput.Instance.GetIsToolkit4)
            {
                IconClick(actionBTNS[3]);
            }
            else if (PlayerInput.Instance.GetIsToolkit5)
            {
                IconClick(actionBTNS[4]);
            }
            else if (PlayerInput.Instance.GetIsToolkit6)
            {
                IconClick(actionBTNS[5]);
            }
            else if (PlayerInput.Instance.GetIsToolkit7)
            {
                IconClick(actionBTNS[6]);
            }
            else if (PlayerInput.Instance.GetIsToolkit8)
            {
                IconClick(actionBTNS[7]);
            }
            else if (PlayerInput.Instance.GetIsToolkit9)
            {
                IconClick(actionBTNS[8]);
            }
            else if (PlayerInput.Instance.GetIsToolkit10)
            {
                IconClick(actionBTNS[9]);
            }
        }

        /// <summary>
        /// 处理按钮点击事件
        /// </summary>
        /// <param name="clickedButton">被点击的按钮</param>
        private void IconClick(Button clickedButton)
        {
            // print("选中: " + clickedButton.gameObject.name);

            CloseAllIcons();
            EnableSelectIcons(clickedButton);
        }

        /// <summary>
        /// 关闭所有按钮的选中状态
        /// </summary>
        private void CloseAllIcons()
        {
            foreach (Button button in actionBTNS)
            {
                Image enableIcon = button.transform.Find("Select").GetComponent<Image>();
                if (enableIcon != null)
                {
                    enableIcon.gameObject.SetActive(false);
                }
            }
        }

        /// <summary>
        /// 激活被点击按钮的选中状态
        /// </summary>
        /// <param name="clickedButton"></param>
        private void EnableSelectIcons(Button clickedButton)
        {
            // 激活被点击按钮的选中状态
            Image enableIconNow = clickedButton.transform.Find("Select").GetComponent<Image>();
            if (enableIconNow != null)
            {
                enableIconNow.gameObject.SetActive(true);
                // print("激活: " + enableIconNow.transform.parent.name);
                clickedButton.gameObject.GetComponent<IKitView>().SetTool();
            }
            else
            {
                Debug.LogError("未找到 'Select' 图标组件");
            }
        }
    }
}